<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Product_categories extends Account_Controller {

    function __construct() {
        parent::__construct();
        //$this->layout->loadLayout("default");
        $this->load->library('rest_caller');
    }

    function index() {
        $url = 'api/product_categories_api/getAll';
        $data = array();
        $result = $this->rest_caller->post($url);
        if ($result->status == false) {
            $data['form_error_message'] = $result->error;
        }
        $data['all_products'] = (isset($result->data) && $result->data) ? $result->data : '';

        $this->layout->view('products/list_all', $data);
    }

    function price_book() {
        $url = 'api/products_api/getAll';
        $data = array();
        $result = $this->rest_caller->post($url);
        if ($result->status == false) {
            $data['form_error_message'] = $result->error;
        }
        $data['all_products'] = (isset($result->data) && $result->data) ? $result->data : '';

        $this->layout->view('products/price_book', $data);
    }

    function edit() {
        
        $product_id = $this->uri->segment(3);
        $url = 'api/products_api/getById';
        $data = array();
        $result = $this->rest_caller->post($url,array('id'=>$product_id));
        if ($result->status == false) {
            $data['form_error_message'] = $result->error;
        }
        $data['product'] = (isset($result->data) && $result->data) ? $result->data : '';
        $this->layout->view('products/edit', $data);
        
    }

    function add() {
        //get cate
        $data['cate'] = array(
            '1' => 'cate1',
            '2' => 'cate2'
        );
        //--
        if ($this->input->server('REQUEST_METHOD') == "POST") {
            $rules = array(
                array(
                    'field' => 'product_code',
                    'label' => 'Mã sản phẩm',
                    'rules' => 'required|max_length[50]'
                ),
                array(
                    'field' => 'product_name',
                    'label' => 'Tên sản phẩm',
                    'rules' => 'required|max_length[255]'
                ),
                array(
                    'field' => 'category',
                    'label' => 'Danh mục sản phẩm',
                    'rules' => 'required|is_natural_no_zero|max_length[11]'
                ),
                array(
                    'field' => 'sale_price',
                    'label' => 'Sale Price',
                    'rules' => 'required|is_natural_no_zero|max_length[11]'
                ),
                array(
                    'field' => 'cost_price',
                    'label' => 'Vendor',
                    'rules' => 'required|is_natural_no_zero|max_length[11]'
                )
            );
            $this->form_validation->set_rules($rules);
            if ($this->form_validation->run() == TRUE) {
                $product = array(
                    'product_code' => $this->input->post('product_code'),
                    'product_name' => $this->input->post('product_name'),
                    'category' => $this->input->post('category'),
                    'sale_price' => $this->input->post('sale_price'),
                    'cost_price' => $this->input->post('cost_price'),
                    'date_manufacture' => $this->input->post('date_manufacture') ? $this->input->post('date_manufacture') : '',
                    'date_expired' => $this->input->post('date_expired') ? $this->input->post('date_expired') : '',
                    'manufacturer' => $this->input->post('manufacturer') ? $this->input->post('manufacturer') : '',
                    'vendor' => $this->input->post('vendor') ? $this->input->post('vendor') : '',
                    'description' => $this->input->post('description') ? $this->input->post('description') : '',
                    'industry' => $this->input->post('industry') ? $this->input->post('industry') : '',
                );
                $url = 'api/products_api/insert';
                $result = $this->rest_caller->post($url, $product);
                if ($result->status == true) {
                    $this->session->set_flashdata('success', "Add product " . $this->input->post('product_name') . ' successfully.');
                    redirect('products');
                } else {
                    $data['form_error_message'] = $result->error;
                }
            }
        }
        $this->layout->view('products/add', $data);
    }

}

?>